package client;

import ihm.Accueil;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RMISecurityManager;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.List;

import remote.ClientInterface;
import remote.PrivateInterface;
import remote.PublicInterface;
import remote.Tweet;

public class Client extends UnicastRemoteObject implements ClientInterface {

	private static final long serialVersionUID = -4184867023466752513L;
	transient public PublicInterface publicAccess;
	transient public PrivateInterface privateAccess;

	private Accueil ihm = null;

	private boolean isMessagePending;

	public Client() throws MalformedURLException, RemoteException,
			NotBoundException {
		System.setSecurityManager(new RMISecurityManager());
		publicAccess = (PublicInterface) Naming
				.lookup("rmi://localhost:2002/PublicAccess");
		privateAccess = null;
		isMessagePending = false;

	}

	public Client(Accueil a) throws MalformedURLException, RemoteException,
			NotBoundException {
		System.setSecurityManager(new RMISecurityManager());
		publicAccess = (PublicInterface) Naming
				.lookup("rmi://localhost:2002/PublicAccess");
		privateAccess = null;
		isMessagePending = false;
		ihm=a;
		

	}

	public static void main(String[] args) throws NotBoundException,
			IOException {
		Client c = new Client();
		// initialize console readerSystem
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		// Used variables
		String Pseudo, Pswd, Tag, Msg;
		boolean exit = false;

		do {
			printOption(c);
			String command = br.readLine();
			if (command.equals("getrecent")) {
				System.out.print("Enter #Tag (or empty): ");
				Tag = br.readLine();
				if (Tag.isEmpty())
					c.getRecent(5);
				else
					c.getRecent(Tag, 5);
			} else if (command.equals("connexion")) {
				System.out.print("Enter Pseudo: ");
				Pseudo = br.readLine();
				System.out.print("Enter Password: ");
				Pswd = br.readLine();
				c.connexion(Pseudo, Pswd);
			} else if (command.equals("inscription")) {
				System.out.print("Enter Pseudo: ");
				Pseudo = br.readLine();
				System.out.print("Enter Password: ");
				Pswd = br.readLine();
				c.inscription(Pseudo, Pswd);
			} else if (command.equals("deconnexion")) {
				c.deconnexion();
			} else if (command.equals("tweeter")) {
				System.out.print("Enter #Tag: ");
				Tag = br.readLine();
				System.out.print("Enter message: ");
				Msg = br.readLine();
				System.out.print("Tweeter ?(yes|no): ");
				if ("yes".equals(br.readLine()))
					c.tweeter(new Tweet(Tag, Msg));
			} else if (command.equals("follow")) {
				System.out.print("Enter User pseudo: ");
				c.follow(br.readLine());
			} else if (command.equals("exit")) {
				exit = true;
			} else {

			}
		} while (exit == false);
	}

	public void follow(String pseudo) throws RemoteException {
		if (privateAccess != null)
			privateAccess.follow(pseudo);
	}

	private void getRecent(String tag, int i) throws RemoteException {
		List<Tweet> tweets = publicAccess.getRecent(tag, i);
		System.out.println("Last 5 tweets of #" + tag + ":");
		System.out.println(tweets);
	}

	private void getRecent(int i) throws RemoteException {
		List<Tweet> tweets = publicAccess.getRecent(i);
		System.out.println("Last 5 tweets :");
		System.out.println(tweets);
	}

	public void tweeter(Tweet tweet) throws RemoteException {
		if (privateAccess != null) {
			privateAccess.tweeter(tweet);
		}
	}

	public void deconnexion() throws RemoteException {
		if (privateAccess != null) {
			privateAccess.deconnexion();
			privateAccess = null;
		}
	}

	private static void printOption(Client c) throws RemoteException {
		System.out.println("Commandes:");
		System.out.println("\tgetrecent");
		if (c.privateAccess == null) {
			System.out.println("\tconnexion");
			System.out.println("\tinscription");
		} else {
			System.out.println("\tdeconnexion");
			System.out.println("\ttweeter");
			System.out.println("\tfollow");
		}
		System.out.println("\texit");

		c.printPendingMessage();
	}

	public boolean connexion(String Pseudo, String Pswd) throws IOException {
		privateAccess = publicAccess.connection(Pseudo, Pswd, this);
		return privateAccess != null;
	}

	public boolean inscription(String Pseudo, String Pswd) throws IOException {
		return publicAccess.inscription(Pseudo, Pswd);
	}

	private void printPendingMessage() throws RemoteException {
		if (isMessagePending && privateAccess != null) {
			System.out.println("Message posté :");
			List<Tweet> pendings = privateAccess.getPendingMessage();
			System.out.println(pendings);
			isMessagePending = false;
		}
	}

	@Override
	public void messagePending() throws RemoteException {
		System.out.println("MessagPending");
		if(ihm!=null) ihm.accueilTweet(null);
		isMessagePending = true;
	}
}
